<script setup lang="ts">
import { getCustomLocalTimeFormat } from 'common/src/utils/time';
import { ElIcon } from 'element-plus';
import { Delete } from '@element-plus/icons-vue';
import { routeColors, routeOptions, TaskType } from 'common/src/utils/special';

interface Props {
  class?: string;
  type: number;
  stage: TaskType;
  eta?: number;
  distance?: number;
  sog?: number;
  roll?: number;
  pitch?: number;
  name?: string;
  deletable?: boolean;
  loading?: boolean;
}

interface Emits {
  (e: 'show-point', type: number): void;
  (e: 'show-attitude', type: number): void;
  (e: 'show-deviation', type: number): void;
  (e: 'delete-route'): void;
  (e: 'show-safety-judge'): void;
}
const emits = defineEmits<Emits>();

const props = withDefaults(defineProps<Props>(), {
  deletable: false,
});

const typeAttr = computed(() => {
  let color = routeColors[props.type] || routeColors[0];
  let name = routeOptions.find(route => route.value === props.type)?.label || props.name || '自定义';
  return { name, color };
});

const attitudeText = computed(() => {
  if (props.roll !== undefined && props.roll > 0) {
    return `${props.roll.toFixed(2)}°/${props.pitch?.toFixed(2)}°`;
  } else {
    return '--';
  }
});
</script>

<template>
  <div :class="`task-route-item ${props.class || ''}`">
    <div class="task-route-item-type">
      <span class="task-route-item-name" :style="{background: typeAttr.color}">
        <span>{{ typeAttr.name }}</span>
        <ElIcon class="task-route-item-delete" v-show="props.deletable" @click="() => emits('delete-route')"><Delete /></ElIcon>
      </span>
      <span>ETA: {{ props.eta ? getCustomLocalTimeFormat(props.eta, 'YYYY-MM-DD HH:mm:ss') : '--' }}</span>
    </div>
    <div class="task-route-item-info">
      <span>距离 {{ props.distance || '--' }}nm</span>
      <span>平均航速 {{ props.sog || '--' }}kts</span>
      <span v-show="props.stage !== TaskType.taskPrepare">最大横纵摇 {{ attitudeText }}</span>
    </div>
    <div class="task-route-item-btns">
      <span v-if="props.eta" @click="() => emits('show-point', props.type)">点位信息</span>
      <span v-if="!props.eta" class="btn-disable">航线预测失败</span>
      <template v-if="props.eta && props.stage === TaskType.taskPrepare">
        <span @click="() => emits('show-safety-judge')">安全评估</span>
      </template>
      <template v-else>
        <template v-if="props.eta">
          <span v-if="(props.roll === undefined) || props.loading" class="btn-disable">姿态预测中</span>
          <span v-else-if="props.roll >= 0" @click="() => emits('show-attitude', props.type)">姿态预测</span>
          <span v-else class="btn-disable">姿态预测失败</span>
        </template>
        <template v-if="props.eta">
          <span v-if="(props.roll === undefined) || props.loading" class="btn-disable">位移计算中</span>
          <span v-else-if="props.roll >= 0" @click="() => emits('show-deviation', props.type)">横向位移</span>
          <span v-else class="btn-disable">位移计算失败</span>
        </template>
      </template>
    </div>
  </div>
</template>

<style lang="less" scoped>
.task-route-item {
  margin-right: 10px;
  padding-top: 12px;
  background: linear-gradient( 53deg, rgba(129,201,255,0.2404) 0%, rgba(120,196,255,0.0762) 100%);
  border-radius: 0px;
  border: 1px solid;
  opacity: 0.9346;
  border-image: linear-gradient(208deg, rgba(217.96235382556915, 240.45918345451355, 247.8204220533371, 0), rgba(178.97035092115402, 221.51232093572617, 237.7783665060997, 0.4698716700077057)) 1 1;
  &:not(:first-child) {
    margin-top: 8px;
  }
  .task-route-item-type {
    padding: 0 12px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-weight: 500;
    font-size: 12px;
    color: rgba(255,255,255,0.5);
    line-height: 16px;
    &>span:first-child {
      padding: 2px 4px;
      color: rgba(255,255,255,0.98);
      border-radius: 4px;
    }
  }
  .task-route-item-info {
    margin-top: 16px;
    padding: 0 12px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-weight: 500;
    font-size: 12px;
    color: #FFFFFF;
    line-height: 16px;
  }
  .task-route-item-name {
    display: flex;
    align-items: center;
  }
  .task-route-item-delete {
    margin-left: 2px;
    cursor: pointer;
  }
  .task-route-item-btns {
    margin-top: 14px;
    display: flex;
    font-weight: 500;
    font-size: 12px;
    color: #1F93FF;
    line-height: 16px;
    border-top: 1px solid rgba(255,255,255,0.13);
    &>* {
      padding: 8px 0;
      flex: 1;
      text-align: center;
      cursor: pointer;
      &:not(:last-child) {
        border-right: 1px solid rgba(255,255,255,0.13);
      }
      &:hover {
        color: #00A6FF;
      }
    }
    .btn-disable {
      color: #ccc;
      cursor: not-allowed;
    }
  }
}
</style>
